package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/15 12:29 下午
 */
public class 剑指_Offer_22_链表中倒数第k个节点 {


    /**
     * 链表尾部截取
     * @param head
     * @param k
     * @return
     */
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode preHead = new ListNode(0), temp = preHead;
        preHead.next = head;
        while (k > 0 && temp.next != null) {
            temp = temp.next;
            k--;
        }
        while (temp.next != null) {
            temp = temp.next;
            preHead = preHead.next;
        }

        return preHead.next;
    }
}
